// 2019 11.18 created by 张永亮

import axios from 'axios';

//根据不同的环境更改不同的baseUrl
let baseUrl;
let token = '';

if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test') {
  baseUrl = 'https://testbid.caigou2003.com/';
} else if (process.env.NODE_ENV === 'production') {
  baseUrl = 'https://bid.caigou2003.com/';
}

axios.defaults.timeout = 20000;
axios.defaults.baseURL = baseUrl;
// axios.defaults.headers.common['token'] = token;
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';

//http request 拦截器
axios.interceptors.request.use(
  config => {
    // config.data = JSON.stringify(config.data);

    token = process.client && localStorage.getItem('token') ? localStorage.getItem('token') :
      process.client && sessionStorage.getItem('token') ? sessionStorage.getItem('token') :
        ''
    // console.log(token)
    config.headers.common['Authorization'] = token;
    return config;
  },
  err => {
    return Promise.reject(err);
  }
);

//响应拦截器及异常处理
axios.interceptors.response.use(response => {
  if (response.data.status) {
    if (parseInt(response.data.status) === 200) {
      // console.log('数据请求成功')
    } else if (parseInt(response.data.status) === 401 || parseInt(response.data.status) === 403) {
      //未登录
      console.log('用户未登录或登录信息失效')
    } else {
      console.log("请求状态异常");
    }
  }

  return response
}, err => {
  if (!(err && err.response)) {
    // eslint-disable-next-line no-console
    console.log('连接到服务器失败')
    console.log(err)
  } else {
    switch (err.response.status) {
      case 400:
        console.log('错误请求')
        break;
      case 401:
        console.log('未授权，请重新登录')
        break;
      case 403:
        console.log('拒绝访问')
        break;
      case 404:
        console.log('请求错误,未找到该资源')
        break;
      case 405:
        console.log('请求方法未允许')
        break;
      case 408:
        console.log('请求超时')
        break;
      case 500:
        console.log('服务器端出错')
        break;
      case 501:
        console.log('网络未实现')
        break;
      case 502:
        console.log('网络错误')
        break;
      case 503:
        console.log('服务不可用')
        break;
      case 504:
        console.log('网络超时')
        break;
      case 505:
        console.log('http版本不支持该请求')
        break;
      default:
        console.log(`连接错误${err.response.status}`)
    }
  }
  return Promise.resolve(err.response)
})


/**
 * 封装get方法
 * @param url
 * @param data
 * @returns {Promise}
 */

export function get(url, params = {}) {
  return new Promise((resolve, reject) => {
    axios.get(url, {
      params: params
    })
      .then(response => {
        resolve(response.data);
      })
      .catch(err => {
        reject(err)
      })
  })
}


/**
 * 封装post请求
 * @param url
 * @param data
 * @param others
 * @returns {Promise}
 */

export function post(url, data = {}, others = {}) {
  // let Data = qs.stringify(data);
  return new Promise((resolve, reject) => {
    axios.post(
      url,
      data,
      others
    )
      .then(response => {
        resolve(response.data);
      }, err => {
        reject(err)
      })

  })
}

/**
 * 封装patch请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function patch(url, data = {}) {
  return new Promise((resolve, reject) => {
    axios.patch(url, data)
      .then(response => {
        resolve(response.data);
      }, err => {
        reject(err)
      })
  })
}

/**
 * 封装put请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function put(url, data = {}) {
  return new Promise((resolve, reject) => {
    axios.put(url, data)
      .then(response => {
        resolve(response.data);
      }, err => {
        reject(err)
      })
  })
}

/**
 * 封装delete请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function _delete(url, data = {}) {
  return new Promise((resolve, reject) => {
    axios.delete(url, data)
      .then(response => {
        resolve(response.data);
      }, err => {
        reject(err)
      })
  })
}

/**
 * 下面是获取数据的接口
 */
export const request = {
  addBid(paramObj = {}) {//后台-添加标讯
    return post('bid/save', paramObj);
  },
  openMember(paramObj = {}) {//后台-通过手机号开通会员
    return post('members/join', paramObj);
  },
  queryMemberStatus(paramObj = {}) {//查询会员开通状态
    return post('members/status', paramObj);
  },
  renewMember(paramObj = {}) {//开通/续费会员
    return post('members/opening/vip', paramObj);
  },
  findByTitle(paramObj = {}) {
    return post('es/findByTitle', paramObj)
  },
  sendSmsCode(paramObj = {}) {//发送短信验证码
    return post('user/sendSmsCode', paramObj)
  },
  register(paramObj = {}) {//用户注册
    return post('user/register', paramObj)
  },
  getBackPassword(paramObj = {}) {//用户注册
    return post('user/forgot/password', paramObj)
  },
  login(paramObj = {}) {//用户登录
    return post('auth/login', paramObj)
  },
  currentUser(paramObj = {}) {//获取用户权限
    return post('user/currentUser', paramObj)
  },
  verificationToken(paramObj = {}) {//验证用户token是否有效
    return get('user/verification', paramObj)
  },

  exportTableTenderingMessage(paramObj = {}) {//导出标讯表格
    // return get('export/bid',paramObj)

    // eslint-disable-next-line no-unused-vars
    /*let url = baseUrl + '/export/bid?';
    for (let key in paramObj){
        if(paramObj[key]){
            url += key + '=' + paramObj[key] + '&';
        }
        console.log(url)
    }
    url = url.substring(0,url.length-1);

    window.open(url,'_blank');*/
    return post('export/back/bid', paramObj, {responseType: 'blob', timeout: 60000 * 3})

  },
  exportSubscribeMessage(paramObj = {}) {//导出获得订阅筛选表格
    return post('export/front/mySubscribe', paramObj, {responseType: 'blob', timeout: 60000 * 3})
  },
  searchCondition(paramObj = {}) {//获取标讯筛选条件列表 name
    return post('dic/condition', paramObj)
  },
  searchConditionWithId(paramObj = {}) {//获取标讯筛选条件列表
    return post('dic/condition/vos', paramObj)
  },
  tenderingMessageList(paramObj = {}) {//获取后台管理标讯列表
    return post('bid/back/list', paramObj)
  },
  deleteTenderingMessage(paramObj = {}) {//删除单条标讯数据
    return post('bid/bidInfo/remove', paramObj)
  },
  batchDeletion(paramObj = {}) {//批量删除标讯管理列表数据
    return post('bid/bidInfo/batch', paramObj)
  },
  tenderingMessageInfo(paramObj = {}) {//获取单条标讯信息
    return get('bid/bidInfo', paramObj)
  },
  updateTenderingMessage(paramObj = {}) {//更新单条标讯信息
    return post('bid/bidInfo/update', paramObj)
  },
  searchAnnouncement(paramObj = {}) {//查询公告类型字典
    return get('dic/announcement', paramObj)
  },
  searchPurchase(paramObj = {}) {//查询采购方式字典
    return get('dic/purchase', paramObj)
  },
  searchPurchaser(paramObj = {}) {//查询采购人字典
    return get('dic/purchaser', paramObj)
  },
  searchRegion(paramObj = {}) {//查询地区
    return get('dic/region', paramObj)
  },
  tagsList(paramObj = {}) {//列表、查询品目接口
    return post('item/listItem', paramObj)
  },
  modifyItemName(paramObj = {}) {//修改品目名称
    return post('item/modifyItemName', paramObj)
  },
  modifyExtendItems(paramObj = {}) {//修改品目近义词
    return post('item/modifyExtendItems', paramObj)
  },
  listItemChildren(paramObj = {}) {//根据品目编码，列表该品目下属所有子品目
    return post('item/listItemChildren', paramObj)
  },
  addItem(paramObj = {}) {//添加品目
    return post('item/addItem', paramObj)
  },
  deleteItems(paramObj = {}) {//批量删除品目
    return post('item/deleteItems', paramObj)
  },

  collectionTaskList(paramObj = {}) {//获取采集器任务列表
    return post('collector/list', paramObj)
  },
  startCollectionTaskList(paramObj = {}) {//批量启动/停止采集器任务列表数据
    return post('collector/artificial/batch', paramObj)
  },


  tradePriceList(paramObj = {}) {//请求交易管理价格列表
    return get('deal/price/list', paramObj)
  },
  modifyPrice(paramObj = {}) {//修改交易管理-定价价格
    return post('deal/modify/price', paramObj)
  },
  discountList(paramObj = {}) {//查询折扣管理列表
    return get('deal/discount/list', paramObj)
  },
  addDiscount(paramObj = {}) {//添加折扣
    return post('deal/add/discount', paramObj)
  },
  deleteDiscount(paramObj = {}) {//删除/批量删除折扣
    return post('deal/delete/discount', paramObj)
  },

  orderList(paramObj = {}) {//查询订单列表(后台)
    // return post('deal/order/list',paramObj)
    return post('deal/order/info/list ', paramObj)
  },
  myOrderList(paramObj = {}) {//查询订单列表(前端)
    return post('deal/order/list/user', paramObj)
  },
  orderDetail(paramObj = {}) {//查询订单详情
    return get('deal/order/query', paramObj)
  },
  orderPayDone(paramObj = {}) {//强制完成订单支付
    return post('deal/corporate/transfer', paramObj)
  },
  deleteOrder(paramObj = {}) {//删除订单
    return post('deal/delete/orders', paramObj)
  },
  payAliPay(paramObj = {}) {//调用支付宝支付
    return post('pay/aliPay', paramObj)
  },
  wxPay(paramObj = {}) {//调用微信支付
    return post('pay/wxPay', paramObj)
  },
  transferPay(paramObj = {}) {
    return post('deal/confirm/transfer', paramObj)
  },
  invoiceList(paramObj = {}) {//查询发票抬头列表
    return post('invoice/back/list', paramObj)
  },
  editInvoice(paramObj = {}) {//查询发票抬头列表
    return post('invoice/edit/invoice', paramObj)
  },
  myInvoiceOrderList(paramObj = {}) {//未开发票列表
    return post('invoice/front/subscribe/orders', paramObj);
  },
  myInvoiceList(paramObj = {}) {//已开发票列表
    return post('invoice/front/list', paramObj);
  },
  myInvoiceDetail(paramObj = {}) {//已开发票详情
    return get('invoice/currency', paramObj);
  },
  addInvoice(paramObj = {}) {//添加发票
    return post('invoice/front/add', paramObj)
  },
  returnInvoiceReason(paramObj = {}) {//获取退票原因列表
    return get('dic/invoice/refund', paramObj)
  },
  returnInvoice(paramObj = {}) {//申请退票
    return post('invoice/front/refund', paramObj)
  },

  addBidAttention(paramObj = {}) {//用户收藏某一标讯
    return post('search/addBidAttention', paramObj)
  },
  cancelBidAttention(paramObj = {}) {//用户取消收藏某一标讯
    return post('search/cancelBidAttention', paramObj)
  },
  queryBidItem(paramObj = {}) {//测试测试测试
    return post('search/queryBidItem', paramObj)
  },

  querySignParam() {//获取加密的key
    return post('search/querySignParam')
  },
  queryBidTitle(paramObj = {}) {//按照标题搜索标讯信息
    return post('search/queryBidTitle', paramObj)
  },
  queryBidContent(paramObj = {}) {//全文搜索标讯信息
    return post('search/queryBidContent', paramObj)
  },
  queryBidTitleInSubscribe(paramObj = {}) {//我的订阅中标讯标题搜索接口
    return post('search/queryBidTitleInSubscribe', paramObj)
  },
  queryBidContentInSubscribe(paramObj = {}) {//我的订阅中标讯全文搜索接口
    return post('search/queryBidContentInSubscribe', paramObj)
  },
  queryBidTitleInAttention(paramObj = {}) {//我的收藏中标讯标题搜索接口
    return post('search/queryBidTitleInAttention', paramObj)
  },
  queryBidContentInAttention(paramObj = {}) {//我的收藏中标讯全文搜索接口
    return post('search/queryBidContentInAttention', paramObj)
  },
  queryBidDetail(paramObj = {}) {//标讯详情
    return post('search/queryBidDetail', paramObj)
  },
  // queryBidDetail(paramObj={}){//标讯详情
  //     console.log("------------访问了标讯详情页---------------");
  //     return post('http://192.168.1.6:8088/search/queryBidDetail',paramObj)
  // },
  listTop2LevelItem(paramObj = {}) {//列表top2品目列表
    return post('item/listTop2LevelItem', paramObj)
  },
  computeSubscribePrice(paramObj = {}) {//计算订阅器价格
    return post('subscribe/computePrice', paramObj)
  },
  submitSubscribe(paramObj = {}) {//提交订阅器
    return post('subscribe/submitSubscribe', paramObj)
  },
  renew(paramObj) {//续费订阅器
    return post('renewals/subscribe', paramObj)
  },
  getBindingCode(paramObj = {}) {//获取微信授权二维码
    return get('wx/user/authorize', paramObj)
  },
  getBindingResult(paramObj = {}) {//获取微信授权结果
    return get('wx/user/bundling/info', paramObj)
  },
  unBinging(paramObj = {}) {//获取微信授权结果
    return get('wx/user/unBundling', paramObj)
  },
  editSubscribe(paramObj = {}) {//编辑订阅器
    return post('subscribe/editSubscribe', paramObj)
  },
  cancelSubscriber(paramObj = {}) {//取消订单
    return post('subscribe/cancel/order', paramObj)
  },
  findSubscriberMessageNum(paramObj = {}) {//查询我的订阅未读消息条数
    return get('subscribe/findSubscribeNum', paramObj)
  },
  clearSubscribeNum(paramObj = {}) {//清空未读订阅消息
    return get('subscribe/clearSubscribeNum', paramObj)
  },
  // findSubscriberMessageNum(paramObj={}){//查询我的订阅未读消息条数
  //     return get('http://192.168.1.6:8088/subscribe/findSubscribeNum',paramObj)
  // },
  // clearSubscribeNum(paramObj={}){//清空未读订阅消息
  //     console.log("________________执行清除操作");
  //     return get('http://192.168.1.6:8088/subscribe/clearSubscribeNum',paramObj)
  // },
  subscriberInfo(paramObj = {}) {//查询订阅器信息
    return post('subscribe/findSubscribe', paramObj)
  },

  hasNewMessage(paramObj = {}) {// 是否有新消息
    return get('message/front/hasMsg', paramObj)
  },
  notice(paramObj = {}) {// 消息中心列表
    return post('/message/front/notice', paramObj)
  },
  read(paramObj = {}) {// 将消息变为已读
    return post('/message/front/read', paramObj)
  },
  currency(paramObj = {}) {// 获取消息详情
    return get('/message/currency', paramObj)
  },
  helpList(paramObj = {}) {// 获取帮助中心列表信息
    return post('/help/currency/list', paramObj)
  },
  helpData(paramObj = {}) {// 根据id查询帮助消息
    return get('/help/currency', paramObj)
  },
  addHelp(paramObj = {}) {//添加帮助中心消息
    return post('/help/back/add', paramObj)
  },
  editHelp(paramObj = {}) {//添加帮助中心消息
    return post('/help/back/edit', paramObj)
  },
  deleteHelp(paramObj = {}) {
    return _delete(`/help/back/${paramObj.id}`)
  },
  userInfo(paramObj = {}) {// 获取用户信息
    return get('/user/query/userInfo', paramObj)
  },
  changeUserInfo(paramObj = {}) {// 修改用户信息
    return post('/user/edit/userInfo', paramObj)
  }
};
